package org.example.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.example.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@RestController
@RequestMapping("/search")
@Slf4j
public class SearchController {
    @Autowired
    SearchService searchService;

    @Autowired
    private RestTemplate restTemplate;
    @PostMapping("/really")
    public List<String> search(@RequestBody String code) throws Exception {
        System.out.println(code);
        List handledInput = searchService.inputHandle(code);
        List codeScores = searchService.searchCode(handledInput);
        List<String> resultHandled = searchService.resultHandle(codeScores);
        for (String s : resultHandled) {
            System.out.println("我要o泡 " + s);
        }
        return resultHandled;
    }
    @PostMapping("/test")
    public List<String> search2(@RequestBody String code) throws Exception {
        System.out.println(code);
        List handledInput = searchService.inputHandle(code);
        List codeScores = searchService.searchCode(handledInput);
        List<String> resultHandled = searchService.resultHandle(codeScores);
        resultHandled.add(0, code);
        List<String> sanitizedResult = new ArrayList<>();
        for (String str : resultHandled) {
            String sanitizedStr = str.replaceAll("\\s+", " ");
            sanitizedResult.add(sanitizedStr);
        }

//        List<String> resultHandled = new ArrayList<>();
//        for (int i =0; i< 20; i++) {
//            resultHandled.add(code);
//        }
//        System.out.println(JSONObject.toJSONString(resultHandled));
        // 设置请求头，声明请求体为JSON类型
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 创建HttpEntity并设置请求头和请求体
        HttpEntity<String> request = new HttpEntity<>(JSONObject.toJSONString(sanitizedResult), headers);
        resultHandled.remove(0);
        List<Double> score = restTemplate.postForObject("http://wwv.east-ai.top/ai", request, List.class);
        System.out.println(score);
        for (String s : sanitizedResult) {
            System.out.println(s);
        }
        // 创建一个映射，将字符串和分数对应起来
        Map<String, Double> map = new HashMap<>();
        for (int i = 0; i < resultHandled.size(); i++) {
            map.put(resultHandled.get(i), score.get(i));
        }


        // 使用比较器对字符串列表进行排序
        Collections.sort(resultHandled, Comparator.comparing(map::get).reversed());

        // 输出排序后的结果
//        for (String str : resultHandled) {
//            System.out.println(str);
//        }
        if(resultHandled.size() < 5)
            return resultHandled;
        return resultHandled.subList(0, 5);
    }
}
